package com.whfc.fuum.redis.impl;

import com.whfc.fuum.redis.WxTokenRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/6 11:05
 */
@Repository
public class WxTokenRedisDaoImpl implements WxTokenRedisDao {

    private static final String WXMP_ACCESS_TOKEN_KEY = "wxmp:access_token::{0}";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setAccessToken(String appType, String accessToken, int expiresIn) {
        String key = MessageFormat.format(WXMP_ACCESS_TOKEN_KEY, appType);
        redisTemplate.opsForValue().set(key, accessToken, expiresIn, TimeUnit.SECONDS);
    }

    @Override
    public String getAccessToken(String appType) {
        String key = MessageFormat.format(WXMP_ACCESS_TOKEN_KEY, appType);
        return redisTemplate.opsForValue().get(key);
    }
}
